package com.java.util;

import com.java.bean.Admin;
import com.java.bean.Courier;
import com.java.bean.User;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class UserUtil {
    public static String getPhone(HttpSession session) {
        Admin admin = (Admin) session.getAttribute("admin");
        Courier courier = (Courier) session.getAttribute("courier");
        return admin != null ? admin.getPhone() : courier == null ? null : courier.getPhone();
    }
    public static String getName(HttpSession session) {
        return ((Admin) session.getAttribute("admin")).getPhone();
    }
    public static Admin getAdmin(HttpSession session) {
        return ((Admin) session.getAttribute("admin"));
    }
    public static User getUser(ServletRequest req) {
        return ((User) ((HttpServletRequest) req).getSession().getAttribute("user"));
    }
    public static Courier getCourier(ServletRequest req) {
        return ((Courier) ((HttpServletRequest) req).getSession().getAttribute("courier"));
    }
    public static Object getUserOrCourier(ServletRequest req) {
        final User user = getUser(req);
        return user == null ? getCourier(req) : user;
    }
}
